package light.head.framework.utils;

import java.io.UnsupportedEncodingException;
import java.util.Collection;
import java.util.List;

import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeUtility;

import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;


/**
 * @author janhang
 * @2012-1-28 下午02:37:00
 */
public class MailUtils {
	
	/**
	 * 发送邮件  收件人一个
	 * @param 	发送邮件的邮件服务器
	 * @param	邮箱用户名
	 * @param	邮箱密码
	 * @param	发送到
	 * @param	发送者
	 * @param 	发送者名称
	 * @param	主题
	 * @param	内容
	 */
	public static boolean send(String hostName, String uname, String pwd, String to, String from, String fromName, String subject, String msg, Collection<InternetAddress> cc) {
		return send(hostName, uname, pwd, to, from, fromName, subject, msg, cc, null, null);
	}

	public static boolean send(String hostName, String uname, String pwd, String to, String from, String fromName, String subject, String msg, Collection<InternetAddress> cc, Boolean isTLS, Boolean isSSL) {
		HtmlEmail email = new HtmlEmail();
		email.setCharset("UTF-8");
		
		//是否TLS校验，，某些邮箱需要TLS安全校验，同理有SSL校验
		if (null != isTLS && isTLS)
			email.setTLS(true);
//		if (null != isSSL && isSSL)
//			email.setSSL(true);
		
		email.setSSL(false);
		
		// 发送邮件的邮件服务器
		email.setHostName(hostName);
		
		// 用户名和密码
		email.setAuthentication(uname, pwd);
		
		try {
			email.addTo(to);
			email.setFrom(from, (null != fromName && !"".equals(fromName) ? fromName : from));
			email.setSubject(subject);
			email.setMsg(msg);
			if (null != cc)
				email.setCc(cc);
			email.send();
			return true;
		} catch (EmailException e) {
			e.printStackTrace();
			return false;
		}
	}
	/**
	 *  发送邮件  收件人多个
	 * @param hostName 发送邮件的邮件服务器
	 * @param uname 邮箱用户名
	 * @param pwd   邮箱密码
	 * @param to    发送到  
	 * @param from  发送者
	 * @param fromName  发送者名称
	 * @param subject   主题
	 * @param msg    内容
	 * @param cc     抄送对象
	 */
	public static void send(String hostName, String uname, String pwd, List<String> to, String from, String fromName, String subject, String msg, Collection<InternetAddress> cc) {
		sendList(hostName, uname, pwd, to, from, fromName, subject, msg, cc, null, null);
	}
	public static void sendList(String hostName, String uname, String pwd, List<String> to, String from, String fromName, String subject, String msg, Collection<InternetAddress> cc, Boolean isTLS, Boolean isSSL) {
		HtmlEmail email = new HtmlEmail();
		email.setCharset("UTF-8");
		
		//是否TLS校验，，某些邮箱需要TLS安全校验，同理有SSL校验
		if (null != isTLS && isTLS)
			email.setTLS(true);
//		if (null != isSSL && isSSL)
//			email.setSSL(true);
		
		email.setSSL(false);
		
		// 发送邮件的邮件服务器
		email.setHostName(hostName);
		
		// 用户名和密码
		email.setAuthentication(uname, pwd);
		
		try {
			// 设置收件人
			for (int i = 0; i < to.size(); i++) {
				email.addTo(((String) to.get(i)), ((String) to.get(i)));
			}
			//email.addTo(to);
			email.setFrom(from, (null != fromName && !"".equals(fromName) ? fromName : from));
			email.setSubject(subject);
			email.setMsg(msg);
			email.setCc(cc);
			email.send();
		} catch (EmailException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 *  发送邮件  收件人多个 且有附件
	 * @param hostName 发送邮件的邮件服务器
	 * @param uname 邮箱用户名
	 * @param pwd   邮箱密码
	 * @param to    发送到  
	 * @param from  发送者
	 * @param fromName  发送者名称
	 * @param subject   主题
	 * @param msg    内容
	 * @param cc     抄送对象
	 */
	public static void sendAttachment(String hostName, String uname, String pwd, List<String> to, String from, String fromName, String subject, String msg, Collection<InternetAddress> cc,String attachPath,String attachName) {
		sendAttachment(hostName, uname, pwd, to, from, fromName, subject, msg, cc, null, null,attachPath,attachName);
	}
	public static void sendAttachment(String hostName, String uname, String pwd, List<String> to, String from, String fromName, String subject, String msg, Collection<InternetAddress> cc, Boolean isTLS, Boolean isSSL,String attachPath,String attachName) {
		HtmlEmail email = new HtmlEmail();
		email.setCharset("UTF-8");
		
		//是否TLS校验，，某些邮箱需要TLS安全校验，同理有SSL校验
		if (null != isTLS && isTLS)
			email.setTLS(true);
//		if (null != isSSL && isSSL)
//			email.setSSL(true);
		
		email.setSSL(false);
		
		// 发送邮件的邮件服务器
		email.setHostName(hostName);
		
		// 用户名和密码
		email.setAuthentication(uname, pwd);
		EmailAttachment attachment = null;
		if (attachPath != null) {
			attachment = new EmailAttachment();
			try {
				attachment.setPath(attachPath);
				attachment.setName(MimeUtility.encodeText(attachName));
				attachment.setDisposition(EmailAttachment.ATTACHMENT);
				attachment.setDescription("Picture of John");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		try {
			// 设置收件人
			for (int i = 0; i < to.size(); i++) {
				email.addTo(((String) to.get(i)), ((String) to.get(i)));
			}
			//email.addTo(to);
			email.setFrom(from, (null != fromName && !"".equals(fromName) ? fromName : from));
			email.setSubject(subject);
			email.setMsg(msg);
			email.setCc(cc);
			if (attachPath != null) {
				email.attach(attachment);
			}
			email.send();
			
		} catch (EmailException e) {
			e.printStackTrace();
		}
	}
}
